package com.fsnbigdata.web;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/api/job", name = "JobController", produces = MediaType.APPLICATION_JSON_VALUE)
public class JobController {

    @Autowired
    private Job importUserJob;

    @Autowired
    private JobLauncher jobLauncher;

    @GetMapping(name = "import", value = "/import")
    public ResponseEntity<?> importCsv()
        throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
        jobLauncher.run(importUserJob,
            new JobParametersBuilder().addLong("uniqueness", System.nanoTime()).toJobParameters());
        return ResponseEntity.ok().build();
    }
}
